package com.cms.freemark;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.cms.common.StringUtils;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

@Component(value="dateDirectiveModel")
public class DateDirectiveModel implements TemplateDirectiveModel {

	private String[]arrs = {"日","一","二","三","四","五","六"};
	
	@Override
	public void execute(Environment ev, Map map, TemplateModel[] model, TemplateDirectiveBody body) throws TemplateException, IOException {
		 StringBuilder sb = new StringBuilder();
		 String dateformat= StringUtils.obj2String(map.get("dateformat"));
		 Date d= new Date();
		 sb.append(new SimpleDateFormat(dateformat).format(d));
		 sb.append("&nbsp;&nbsp;");
		 sb.append(this.calcWeek(d));
		 ev.getOut().write(sb.toString());
		 ev.getOut().flush();
	}
	
	
	/***
	 * 
	 * @param d
	 * @return
	 */
	private String calcWeek(Date d)
	{
		StringBuilder sb = new StringBuilder();
		return sb.append("星期").append(arrs[d.getDay()]).toString();
	}

}
